import Cookie from 'js-cookie'
export default {
    state:{
        isCollapse:false, //用于控制菜单的展开或收起
        tabsList:[
            {
                path:'/',
                name:"home",
                label:"首页",
                icon:"s-home",
                url:"Home/home"
              },
        ] ,  //面包屑
        meun:[]
    },
    mutations:{
        //修改菜单展开或收起
        collapseMeun(state){
            state.isCollapse=!state.isCollapse
        },
        //修改更新面包屑数据
        selectMeun(state,val){
            // console.log(val)
            if(val.name!=='home')
            {
                const index=state.tabsList.findIndex(item=>item.name===val.name)
                if(index===-1)
                state.tabsList.push(val)
            }
        },
        closeTag(state,item){
            // console.log(item)
            const index= state.tabsList.findIndex(val=>val.name===item.name)
            state.tabsList.splice(index,1)
        },
        // 设置meun
        setMeun(state,val){
            state.meun=val
            // console.log('store',JSON.stringify(val))
            Cookie.set('meun',JSON.stringify(val))
        },
        addMeun(state,router){
            if(!Cookie.get('meun')){
                const meun=JSON.parse(Cookie.get('meun'))
                state.meun=meun
                let meunArray=[]
                console.log('tab01')
                meun.forEach(item => {
                    if(item.children){
                       item.children= item.children.map(item=>{
                            item.component=()=>import(`../views/${(item.url)}`)
                            return item
                        })
                        meunArray.push(...item.children)
                        console.log(meunArray)
                    }else{
                        item.component=()=>import(`../views/${(item.path)}`)
                        meunArray.push(item)
                        console.log(meunArray)

                    }
                })
                console.log(meunArray,'meunArray')
                meunArray.forEach(item=>{
                    router.addRoute('Main',item)
                    console.log(router)
                })
            }
        }
    }
}